//
//  ByCellAddAddress.m
//  oc_knowledge
//
//  Created by mc on 17/1/12.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellAddAddress.h"
//View
#import "ByViewTextViewPlaceholder.h"

#define LabelHeight    40
@interface ByCellAddAddress()<UITextFieldDelegate,UITextViewDelegate>
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *subTitles;
@property (nonatomic, strong) UITextField *tf;
@property (nonatomic, strong) ByViewTextViewPlaceholder *detailView;
@end
@implementation ByCellAddAddress

+ (instancetype)cellWithTableView:(UITableView*)tableView andTitles:(NSArray *)titles andPlaceHolder:(NSArray *)subtitles andType:(NSInteger)type{
    static NSString *cellIdentifier = @"ByCellAddAddress";
    ByCellAddAddress *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier andTitles:titles andPlaceHolder:subtitles andType:type];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andTitles:(NSArray *)titles
andPlaceHolder:(NSArray *)subtitles andType:(NSInteger)type{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        _titles = titles;
        _subTitles = subtitles;
        _type = type;
        [self setViews];

    }
    return self;
}




#pragma mark ---创建视图
- (void)setViews{
    CGFloat maxWidth = 0;
    /***循环寻找最长的title*/
    for (NSString *str in _titles) {
        CGFloat width = [str getSizeOfString:16].width;
        maxWidth = width > maxWidth ? width:maxWidth;
    }
    CGFloat x,y,width;
    width = maxWidth + 15;
    x = width + 10;
    //循环创建title
    for (int i = 0; i < _titles.count; i ++) {
        y = 10 * (i + 1) + (LabelHeight + 10) * i;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, y, width, LabelHeight)];
        titleLabel.text = _titles[i];
        titleLabel.font = FONT(16);
        titleLabel.textColor = [UIColor blackColor];
        [self addSubview:titleLabel];
        
        /**所在地区*/
        if ([_titles[i] containsString:@"地区"]) {
            /**箭头*/
            UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30 - 10, y + 10, 20, 20)];
            img.image = [UIImage imageNamed:@"Order_arrow"];
            [self addSubview:img];
            
            UILabel *areaLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, SCREEN_WIDTH - x - 45, LabelHeight)];
            areaLabel.tag = 10 + i;
            areaLabel.font = FONT(14);
            if (_type == 0) {
                areaLabel.text = _subTitles[i];
                areaLabel.textColor = [UIColor colorWithWhite:0.80 alpha:1];
            }else{
                areaLabel.textColor = [UIColor blackColor];
            }
            [self addSubview:areaLabel];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(areaSelect:)];
            areaLabel.userInteractionEnabled = YES;
            [areaLabel addGestureRecognizer:tap];

        }
        /**其他*/
        else{
            
            _tf = [[UITextField alloc] initWithFrame:CGRectMake(x, y, SCREEN_WIDTH - x, LabelHeight)];
            _tf.placeholder = _subTitles[i];
            _tf.font = FONT(14);
            if ([_titles[i] isEqualToString:@"手机号码"]) {
                _tf.keyboardType = UIKeyboardTypeNumberPad;
            }
            _tf.delegate = self;
            _tf.tag = 10 + i;
            [self addSubview:_tf];
        }
        
        //线
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, titleLabel.bottom + 9, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
        [self addSubview:lineView];
        
    }
    
    //创建详细地址
    x = 10;
    y = _titles.count * 2 *10 + LabelHeight * _titles.count;
    _detailView = [[ByViewTextViewPlaceholder alloc] initWithFrame:CGRectMake(x, y, SCREEN_WIDTH - 2*x, 60)];
    _detailView.font = FONT(14);
    _detailView.delegate = self;
    _detailView.tag = 10 + self.titles.count;
    _detailView.placeholder = _subTitles.lastObject;
    [self addSubview:_detailView];
    

}
#pragma mark ---设置参数
-(void)setModel:(ByModelAddress *)model{
    _model = model;
    /**遍历所有的输入框*/
    UITextField *tf;
    for (int i = 0; i < self.titles.count; i ++) {
        //收货人
        if ([self.titles[i] containsString:@"收货人"]) {
            tf = [self viewWithTag:i + 10];
            tf.text = _model.contactname;
        }
        //手机号码
        if ([self.titles[i] containsString:@"手机号"]) {
            tf = [self viewWithTag:i + 10];
            tf.text = _model.mobile;
        }
        //选择地区
        if ([self.titles[i] containsString:@"地区"]) {
            UILabel *areaLabel = [self viewWithTag:i + 10];
            if ([_model.city isEqualToString:@"市辖区"] || [_model.city isEqualToString:@"县"]) {
                areaLabel.text = [NSString stringWithFormat:@"%@%@",_model.province,model.area];
            }else{
                areaLabel.text = [NSString stringWithFormat:@"%@%@%@",_model.province,_model.city,model.area];
            }
        }

    }
    
    //详细地址
    _detailView.text = _model.detailinfo;
    
    
}
- (void)areaSelect:(UITapGestureRecognizer *)tap{
    NSLog(@"%ld",(long)_tf.tag);
    /**遍历所有输入框  只要不是地区选择  取消第一响应者*/
    for (int i = 0; i < self.titles.count; i ++) {
        if (![self.titles[i] containsString:@"地区"]) {
            UITextField *tf = [self viewWithTag:10 + i];
            [tf resignFirstResponder];
        }
    }
    /**释放详细地址  输入框响应*/
    [_detailView resignFirstResponder];
    if (self.areaSelect) {
        self.areaSelect();
    }
}
#pragma mark ---textfieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (self.begainEdit) {
        self.begainEdit();
    }
}
-(void)textViewDidBeginEditing:(UITextView *)textView{
    if (self.begainEdit) {
        self.begainEdit();
    }
}

@end
